package com.cloudptt.wp.ui.address.map.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.osmdroid.views.MapView;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Path.Direction;
import android.graphics.Region.Op;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class RoundMenuView extends View {
	private float coreX; // ���ĵ��x����
	private float coreY; // ���ĵ�y

	private List<RoundMenu> roundMenus;
	private boolean isCoreMenu = true; // �Ƿ������İ�ť
	private int coreMenuColor; // ���İ�ťĬ�ϱ���
	private int coreMenuStrokeColor; // ���İ�ť����ɫ
	private int coreMenuStrokeSize; // ���İ�ť�ߴ�ϸ
	private int coreMenuSelectColor; // ���İ�ťѡ��ʱ�ı�����ɫ
	private Bitmap coreBitmap; // OKͼƬ
	private OnClickListener onCoreClickListener; // ���İ�ť�ĵ���ص�
    public Context mContext;
	private float deviationDegree; // ƫ��
	private int onClickState = -2; // -2���޵����-1�ǵ������Բ�������ǵ���˵�
	private int roundRadius; // ����Բ�İ뾶
	private double radiusDistance; // �뾶�ĳ��ȱ�
	private long touchTime; // ����ʱ��
	public boolean clickMenu1 = false;
	public boolean clickMenu2 = false;
	public boolean clickMenu3 = false;
	public boolean clickMenu4 = false;
	public boolean clickMenu5 = false;
	private int width;
	private int height;
	public MapView map;
	public static int sLineColorIndex = 1;
	public RoundMenuView(Context context) {
		this(context, null);
	}
	public RoundMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		width = 350;
		height = 350;
	}
	public RoundMenuView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	public void onMeasure(int widthMeasure, int heightMeasure) {
		setMeasuredDimension(width, height);
	}
	protected void onDraw(Canvas canvas) {
		this.coreX = width/2;
		this.coreY = height/2;
		roundRadius = (int) ((width / 2) * radiusDistance); // ��������ԲȦ�뾶
		RectF rect = new RectF(coreX - 150, coreY - 150, coreX + 150, coreY+150);
		Path path = new Path();
		path.addCircle(coreX, coreY, roundRadius, Direction.CCW);
		canvas.clipPath(path,Op.DIFFERENCE);
		if (roundMenus != null && roundMenus.size() > 0) {
			float sweepAngle = 360 / roundMenus.size();// ÿ�����εĽǶ�
			deviationDegree = sweepAngle / 2; // ��ʵ��ƫ�ƽǶ�
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(getResources().getColor(R.color.color_gray));
			canvas.drawArc(rect, 0, 360, true, paint);
			for (int i = 0; i < roundMenus.size(); i++) {
				RoundMenu roundMenu = roundMenus.get(i);
				Matrix matrix = new Matrix();
				if(roundMenu.icon != null){
				matrix.postTranslate(
						(float) ((coreX + getWidth() / 2 * roundMenu.iconDistance) - (roundMenu.icon.getWidth() / 2)),
						coreY - (roundMenu.icon.getHeight() / 2));
				matrix.postRotate(((i + 1) * sweepAngle), coreX, coreY);
				canvas.drawBitmap(roundMenu.icon, matrix, null);
				}
			}
		} else if(roundMenus != null && roundMenus.size() == 0){
			clickMenu1 = false;
			clickMenu2 = false;
			clickMenu3 = false;
			clickMenu4 = false;
		}
		if (isCoreMenu) {
			RectF rect1 = new RectF(coreX - roundRadius, coreY - roundRadius, coreX + roundRadius, coreY + roundRadius);
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setStrokeWidth(coreMenuStrokeSize);
			paint.setColor(getResources().getColor(R.color.transparent));
			canvas.drawArc(rect1, 0, 360, true, paint);
			if (coreBitmap != null) {
				canvas.drawBitmap(coreBitmap, coreX - coreBitmap.getWidth() / 2, coreY - coreBitmap.getHeight() / 2,null);
			}
		}
	}
	
	public void setMapView(MapView map){
		this.map = map;
	}
	
    public MapView getMapView() {
        return map;
     }

	public void addRoundMenu(RoundMenu roundMenu) {
		if (roundMenu == null) {
			return;
		}
		if (roundMenus == null) {
			roundMenus = new ArrayList<RoundMenu>();
		}
		roundMenus.add(roundMenu);
		// invalidate();
	}
	
	public RoundMenu getRoundMenu(int i) {
		if(roundMenus !=null && roundMenus.size()>0){
			return roundMenus.get(i);
		}
		return null;
	}
	
	public void removeView() {
		if (roundMenus != null) {
			if (roundMenus.size() >= 0) {
				roundMenus.clear();
			}
		}
	}
	public RoundMenu getView(int i){
		if(roundMenus != null){
			if(roundMenus.size()>=0){
				return roundMenus.get(i);
			}
		}
		return null;
	}
	

	public void setCoreMenu(int coreMenuStrokeSize,double radiusDistance,int coreMenuStrokeColor,int coreMenuSelectColor,int coreMenuColor) {
		isCoreMenu = true;
		this.coreMenuColor = coreMenuColor;
		this.radiusDistance = radiusDistance;
		this.coreMenuSelectColor = coreMenuSelectColor;
		this.coreMenuStrokeColor = coreMenuStrokeColor;
		this.coreMenuStrokeSize = coreMenuStrokeSize;
	}

	public static class RoundMenu {
		public boolean useCenter = false;
		public int solidColor = Color.GRAY;
		public int selectSolidColor = Color.GRAY; // ������ɫ��Ĭ��͸��
		public int strokeColor = Color.GRAY;
		public int strokeSize = 2;
		public Bitmap icon;
		public OnClickListener onClickListener;
		public double iconDistance = 0.70; // ͼ��������ĵ�ľ���
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			touchTime = new Date().getTime();
			float textX = event.getX();
			float textY = event.getY();
			int distanceLine = (int) getDisForTwoSpot(coreX, coreY, textX, textY);
			if (distanceLine <= roundRadius) {
				onClickState = -1;
				return false;
			}
		 if (distanceLine <= 150 && roundMenus != null) {
				if (roundMenus.size() > 0) {
					float sweepAngle = 360 / roundMenus.size();
					int angle = getRotationBetweenLines(coreX, coreY, textX, textY);
					angle = (angle + 360 - 90 - (int) deviationDegree) % 360;
					onClickState = (int) (angle / sweepAngle);
					showClickMenu(onClickState);
				}
		 }
		break;
		case MotionEvent.ACTION_UP:
			if ((new Date().getTime() - touchTime) < 300) {
				OnClickListener onClickListener = null;
				if (onClickState == -1) {
					onClickListener = onCoreClickListener;
				} 
				else if (onClickState >= 0 && onClickState < roundMenus.size()) {
					onClickListener = roundMenus.get(onClickState).onClickListener;
				}
				if (onClickListener != null) {
					onClickListener.onClick(this);
				}
			}  
			onClickState = -2;
			break;
		}
		return true;
	}
	
	public void showClickMenu(int onClickState){
		switch (onClickState){
		case 0:
			if(clickMenu1 == false){
				showIcon();
				roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.more_info_select));
				clickMenu1 = true;  //�������֮�󣬸ñ�־һֱ��ʾ�Ѿ�����
				clickMenu2 = false;
				clickMenu3 = false;
				clickMenu4 = false;
				invalidate();
			}
			break;
		case 1:
			if(clickMenu2 == false){
				showIcon();
				roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.more_info_select));
				invalidate();
				clickMenu2 = true;
				clickMenu3 = false;
				clickMenu4 = false;
				
			} else if(clickMenu2 == true){
				roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.more_info_normal));
				clickMenu2 = false;
				invalidate();
			}
			break;
		case 2:
			if(clickMenu3 == false){
				showIcon();
			//	roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.button_add_select));
				invalidate();
				clickMenu3 = true;
				clickMenu2 = false;
				clickMenu4 = false;
			} else if(clickMenu3 == true){
			//	roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.button_add_normal));
				clickMenu3 = false;
				invalidate();
			}
			break;
		case 3:
			if(clickMenu4 == false){
				showIcon();
				roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.button_delete_normal));
				invalidate();
				clickMenu4 = true;
				clickMenu2 = false;
				clickMenu3 = false;
			} else if(clickMenu4 == true){
				roundMenus.get(onClickState).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.button_delete_normal));
				clickMenu4 = false;
				invalidate();
			}
			break;
		}
	}
	
	public void showIcon(){
		//roundMenus.get(0).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.more_info_normal));
		roundMenus.get(1).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.more_info_normal));
	//	roundMenus.get(2).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.button_add_normal));
		roundMenus.get(3).icon = drawable2Bitmap(mContext, getResources().getDrawable(R.drawable.button_delete_normal));
	}
	
	public Bitmap drawable2Bitmap(Context context, Drawable drawable) {
		if (drawable instanceof BitmapDrawable) {
			return ((BitmapDrawable) drawable).getBitmap();
		} else if (drawable instanceof NinePatchDrawable) {
			Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
					drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
			drawable.draw(canvas);
			return bitmap;
		} else {
			return null;
		}
	}
		 
		 
		 public static int getRotationBetweenLines(float centerX, float centerY, float xInView, float yInView) {
				double rotation = 0;
				double k1 = (double) (centerY - centerY) / (centerX * 2 - centerX);
				double k2 = (double) (yInView - centerY) / (xInView - centerX);
				double tmpDegree = Math.atan((Math.abs(k1 - k2)) / (1 + k1 * k2)) / Math.PI * 180;
				if (xInView > centerX && yInView < centerY) {
					rotation = 90 - tmpDegree;
				} else if (xInView > centerX && yInView > centerY) {
					rotation = 90 + tmpDegree;
				} else if (xInView < centerX && yInView > centerY) {
					rotation = 270 - tmpDegree;
				} else if (xInView < centerX && yInView < centerY) {
					rotation = 270 + tmpDegree;
				} else if (xInView == centerX && yInView < centerY) {

					rotation = 0;
				} else if (xInView == centerX && yInView > centerY) {
					rotation = 180;
				}
				return (int) rotation;
			}

			// ������֮��ľ���
			public static double getDisForTwoSpot(float x1, float y1, float x2, float y2) {
				float width, height;
				if (x1 > x2) {
					width = x1 - x2;
				} else {
					width = x2 - x1;
				}
				if (y1 > y2) {
					height = y2 - y1;
				} else {
					height = y2 - y1;
				}
				return Math.sqrt((width * width) + (height * height));
			}
	
}
